<template>
	<view class="content">
		<button v-if="!userinfo" class="textCen " open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			<view style="height: 150rpx;"></view>
			<image class="logo" src="../../static/server/serverLog.png"></image>
			<view class="title foSi50">城市服务方</view>
		</button>
		<view v-if="userinfo" class="textCen " @click="bindPhone">
			<view style="height: 150rpx;"></view>
			<image class="logo" src="../../static/server/serverLog.png"></image>
			<view class="title foSi50">城市服务方</view>
		</view>
		
		
		
		<view v-if="Object.keys(device_userinfo).length > 0" class="textCen " @click="bindPhone2">
			<view style="height: 150rpx;"></view>
			<image class="logo" src="../../static/server/serverLog.png"></image>
			<view class="title foSi50">设备管理</view>
		</view>
		<button v-else class="textCen " open-type="getPhoneNumber" @getphonenumber="getPhoneNumber2">
			<view style="height: 150rpx;"></view>
			<image class="logo" src="../../static/server/serverLog.png"></image>
			<view class="title foSi50">设备管理</view>
		</button>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: '',
				device_userinfo:{}
			}
		},
		onLoad() {
		},
		onShow(){
			this.userinfo =  uni.getStorageSync('userinfo')
			this.device_userinfo =  uni.getStorageSync('device_userinfo')
		},
		methods: {
			bindPhone(){
				uni.setStorageSync('token',uni.getStorageSync('lyk_token'))
				uni.setStorageSync('userinfo',uni.getStorageSync('lyk_userinfo'))
				uni.setStorageSync('system_type',1)
				uni.navigateTo({
					url: '/pages/personalCenter/personalCenter'
				});
			},
			
			
			bindPhone2(){
				uni.setStorageSync('token',uni.getStorageSync('device_token'))
				uni.setStorageSync('userinfo',uni.getStorageSync('device_userinfo'))
				uni.setStorageSync('system_type',2)
				uni.navigateTo({
					url: '/pages/personalCenter/personalCenter_device'
				});
			},
			getPhoneNumber (e) {
				console.log(e)
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				
				uni.login({
				  success: function (loginRes) {
				    console.log(loginRes);
					uni.request({
						url: that.global.url+'api/user/get_phone',
						method: 'POST',
						data: {
						    code:e.detail.code,
							openidcode:loginRes.code
						},
						success(res) {
						    let result = res.data
						    console.log(result)
						    if(result.code == 1){
						       that.login(result.data)
						    }else{
						        uni.showToast({
						            title: result.msg,
						            icon:'none'
						          })
						    }
						},complete() {
							uni.hideLoading()
						}
					})
				  }
				});
				
				
				
				// this.login(18143079335)
			},
			login(mobile){
				
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				uni.request({
					url: this.global.url+'api/user/mobilelogin',
					method: 'POST',
					data: {
					    mobile:mobile
					},
					success(res) {
					    let result = res.data
					    console.log(result)
					    if(result.code == 1){
					        // 存用户信息
					        uni.setStorageSync('lyk_userinfo', result.data.userinfo)
							// 存用户信息
							uni.setStorageSync('lyk_token', result.data.userinfo.token)
							that.bindPhone()
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none'
					          })
					    }
					},complete() {
						uni.hideLoading()
					}
				})
			},
			
			
			
			
			
			// 设备登录
			getPhoneNumber2(e) {
				console.log(e)
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				
				uni.login({
				  success: function (loginRes) {
				    console.log(loginRes);
					uni.request({
						url: that.global.url+'api/user/get_phone',
						method: 'POST',
						data: {
						    code:e.detail.code,
							openidcode:loginRes.code
						},
						success(res) {
						    let result = res.data
						    console.log(result)
						    if(result.code == 1){
								that.login2(15205326556)
						       // that.login2(result.data)
						    }else{
						        uni.showToast({
						            title: result.msg,
						            icon:'none'
						          })
						    }
						},complete() {
							uni.hideLoading()
						}
					})
				  }
				});
				// this.login(18143079335)
			},
			
			
			
			login2(mobile){
				
				var that = this
				uni.showLoading({
					title:'加载中...'
				})
				uni.request({
					url: this.global.url+'api/user/mobilelogin',
					method: 'POST',
					data: {
					    mobile:mobile,
						system_type:2,
					},
					success(res) {
					    let result = res.data
					    console.log(result)
					    if(result.code == 1){
					        // 存用户信息
					        uni.setStorageSync('device_userinfo', result.data.userinfo)
							// 存用户信息
							uni.setStorageSync('device_token', result.data.userinfo.token)
							that.bindPhone2()
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none'
					          })
					    }
					},complete() {
						uni.hideLoading()
					}
				})
			},
			
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background-color: #F0F0F0;
	}

	.logo {
		height: 295rpx;
		width: 308rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		color: #000000;
	}
	button::after {
		border: none;
	}
	button {
		display: block;
		margin-left: auto;
		margin-right: auto;
		padding-left: 0px;
		padding-right: 0px;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		background-color: #F0F0F0;
	}
</style>
